home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / include / macros.asm < prev    next >
Encoding:
Assembly Source File  |  2001-07-03  |  22.0 KB  |  1,304 lines

  1. ;_ macros.asm
  2. ; Copyright (C) 1985-1991 by Walter Bright
  3. ; All Rights Reserved
  4. ; Modified for COFF and ELF support by David Bustin
  5. ; $Revision: 1.1.1.1 $ 
  6.  
  7. ifdef _WIN32
  8. ifndef __NT__
  9. __NT__ equ 1
  10. endif
  11. endif
  12.  
  13. ifndef M_I386
  14.  
  15. ifdef _WINDOWS
  16.     extrn  MESSAGEFATALERROR : far
  17. endif
  18.  
  19. ; modified for RATIONAL support by P Murray, April 1990
  20. ; define DOS16RM for protected mode programs
  21.  
  22. ; Determine which memory model we are assembling for. For .COM files,
  23. ; force S model.
  24.  
  25. ifdef DOS16RM
  26. ifndef I8086L
  27. %out    Only L model supported with RATIONAL DOS Extender.
  28. end
  29. endif
  30. else
  31.     ifdef I8086T
  32. I8086S equ    1
  33.     else
  34.     ifndef I8086S
  35.     ifndef I8086M
  36.     ifndef I8086C
  37.     ifndef I8086L        ;if none of the memory models are defined
  38.     ifndef I8086V
  39. I8086S equ    1        ;default to S model
  40.     endif ;I8086V
  41.     endif ;I8086L
  42.     endif ;I8086C
  43.     endif ;I8086M
  44.     endif ;I8086S
  45.     endif ;I8086T
  46. endif ;DOS16RM
  47.  
  48. ifdef __OS2__
  49.     .286C
  50. endif
  51.  
  52.  
  53. ifdef DOS16RM
  54.     .286P
  55. endif
  56.  
  57. ifndef I386
  58. I386    equ    0
  59. endif
  60.  
  61. ;Decide if SI and DI are saved across function calls
  62. SAVESIDI equ    1        ;1 means SI and DI are saved across functions
  63.  
  64. if 0    ;Lattice conventions no longer supported
  65. MSC    equ    1        ;ifdef means use Microsoft C calling conventions
  66.                 ;ifndef means use Lattice
  67. endif
  68.  
  69. ; Macros to bracket data segment stuff.
  70.  
  71. ifndef STARTUP
  72. begdata macro
  73.  
  74. ifdef DOS16RM
  75. ;Segment so we can find the start of DGROUP
  76.  
  77. NULL    segment para public 'BEGDATA'        ;Note PARAGRAPH alignment
  78. NULL    ends
  79. endif
  80.  
  81. _DATA    segment word public 'DATA'
  82. _DATA    ends
  83. CONST    segment word public 'CONST'
  84. CONST    ends
  85. _BSS    segment word public 'BSS'
  86. _BSS    ends
  87. ifdef DOS16RM
  88. DGROUP    group    NULL,_DATA,CONST,_BSS
  89. else
  90. DGROUP    group    _DATA,CONST,_BSS
  91. endif
  92. _DATA    segment
  93.     assume ds:DGROUP
  94.     endm
  95. endif ; STARTUP
  96.  
  97. enddata macro
  98. _DATA    ends
  99.     endm
  100.  
  101. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  102. ; Macros specific to each memory model in an attempt to make it easier
  103. ; to write memory model independent code.
  104. ;    begcode,endcode        Use to bracket code sections
  105. ;    P            Offset on BP to first argument on stack
  106. ;                (excluding any local variables)
  107. ;    SPTR            1 if small data model
  108. ;    LPTR            1 if large pointers (large data)
  109. ;    LCODE            1 if large code model
  110. ;    ESeqDS            1 if ES == DS at all times
  111. ;    SSeqDS            1 if SS == DS at all times
  112. ;    SIZEPTR            # of bytes in a pointer
  113. ;    func            Declare a function as NEAR or FAR
  114. ;    callm            Call function as NEAR or FAR
  115.  
  116. ;;;;;;;;;;;;;; SMALL MEMORY MODEL ;;;;;;;;;;;;;;;;;
  117. ifdef I8086S
  118. begcode macro    module
  119. _TEXT    segment word public 'CODE'
  120.     assume    cs:_TEXT
  121.     endm
  122.  
  123. endcode macro    module
  124. _TEXT    ENDS
  125.     endm
  126.  
  127. P    equ    4    ; Offset of start of parameters on the stack frame
  128. SPTR    equ    1
  129. LPTR    equ    0
  130. LCODE    equ    0
  131. ESeqDS    equ    0
  132. SSeqDS    equ    1
  133. SIZEPTR equ    2    ; Size of a pointer
  134.  
  135. p_func    macro    name
  136. name    proc    near
  137.     endm
  138.  
  139. p_endp    macro    name
  140. name    endp
  141.     endm
  142.  
  143. callp    macro    name
  144.     call    near ptr name
  145.     endm
  146.  
  147. func    macro    name
  148. _&name    proc    near
  149.     ifndef name
  150. name    equ    _&name
  151.     endif
  152.     endm
  153.  
  154. callm    macro    name
  155.     call    near ptr _&name
  156.     endm
  157. endif
  158.  
  159. ;;;;;;;;;;;;;;;;; MEDIUM MEMORY MODEL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  160.  
  161. ifdef I8086M
  162. begcode macro    module
  163. module&_TEXT    segment para public 'CODE'
  164.     assume    cs:module&_TEXT
  165.     endm
  166.  
  167. endcode macro    module
  168. module&_TEXT    ends
  169.     endm
  170.  
  171. P    equ    6    ; Offset of start of parameters on the stack frame
  172. SPTR    equ    1
  173. LPTR    equ    0
  174. LCODE    equ    1
  175. ESeqDS    equ    0
  176. SSeqDS    equ    1
  177. SIZEPTR equ    2
  178.  
  179. p_func    macro    name
  180. name    proc    far
  181.     endm
  182.  
  183. p_endp    macro    name
  184. name    endp
  185.     endm
  186.  
  187. callp    macro    name
  188.     call    far ptr name
  189.     endm
  190.  
  191. func    macro    name
  192. _&name    proc    far
  193.     ifndef name
  194. name    equ    _&name
  195.     endif
  196.     endm
  197.  
  198. callm    macro    name
  199.     call    far ptr _&name
  200.     endm
  201. endif
  202.  
  203. ;;;;;;;;;;;;;;;;; COMPACT MEMORY MODEL ;;;;;;;;;;;;;;
  204.  
  205. ifdef I8086C
  206. begcode macro    module
  207. _TEXT    segment word public 'CODE'
  208.     assume    cs:_TEXT
  209.     endm
  210.  
  211. endcode macro    module
  212. _TEXT    ends
  213.     endm
  214.  
  215. P    equ    4    ; Offset of start of parameters on the stack frame
  216. SPTR    equ    0
  217. LPTR    equ    1
  218. LCODE    equ    0
  219. ESeqDS    equ    0
  220. SSeqDS    equ    0
  221. SIZEPTR equ    4
  222.  
  223. p_func    macro    name
  224. name    proc    near
  225.     endm
  226.  
  227. p_endp    macro    name
  228. name    endp
  229.     endm
  230.  
  231. callp    macro    name
  232.     call    near ptr name
  233.     endm
  234.  
  235. func    macro    name
  236. _&name    proc    near
  237.     ifndef name
  238. name    equ    _&name
  239.     endif
  240.     endm
  241.  
  242. callm    macro    name
  243.     call    near ptr _&name
  244.     endm
  245. endif
  246.  
  247. ;;;;;;;;;;;;;;;; LARGE MEMORY MODEL ;;;;;;;;;;;;;;;;;;;
  248.  
  249. ifdef I8086L
  250. begcode macro    module
  251. module&_TEXT    segment para public 'CODE'
  252.     assume    cs:module&_TEXT
  253.     endm
  254.  
  255. endcode macro    module
  256. module&_TEXT    ends
  257.     endm
  258.  
  259. P    equ    6    ; Offset of start of parameters on the stack frame
  260. SPTR    equ    0
  261. LPTR    equ    1
  262. LCODE    equ    1
  263. ESeqDS    equ    0
  264. SSeqDS    equ    0
  265. SIZEPTR equ    4
  266.  
  267. p_func    macro    name
  268. name    proc    far
  269.     endm
  270.  
  271. p_endp    macro    name
  272. name    endp
  273.     endm
  274.  
  275. callp    macro    name
  276.     call    far ptr name
  277.     endm
  278.  
  279. func    macro    name
  280. _&name    proc    far
  281.     ifndef name
  282. name    equ    _&name
  283.     endif
  284.     endm
  285.  
  286. callm    macro    name
  287.     call    far ptr _&name
  288.     endm
  289. endif
  290.  
  291. ;Macros to replace public, extrn, and endp for C-callable assembly routines,
  292. ; and to define labels: c_label defines labels,
  293. ; c_public replaces public, c_extrn replaces extrn, and c_endp replaces endp
  294.  
  295. c_name    macro    name
  296.     name equ _&name
  297.     endm
  298.  
  299. c_label macro    name
  300. _&name:
  301.     endm
  302.  
  303. c_public macro    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  304.     ifnb <a>            ;;Check for blank argument
  305.     public    _&a
  306.     a equ _&a
  307.       ifnb <b>
  308.     c_public b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  309.       endif
  310.     endif
  311.     endm
  312.  
  313. c_extrn macro    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  314.     ifnb <a>            ;;Check for blank argument
  315.     extrn    _&a:b
  316.     a equ _&a
  317.       ifnb <c>
  318.     c_extrn c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  319.       endif
  320.     endif
  321.     endm
  322.  
  323. c_endp    macro    name
  324. _&name    ENDP
  325.     endm
  326.  
  327. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  328. ; Define function ctor as a static constructor
  329. static_ctor macro    ctor
  330.     if LCODE
  331. XIFB    segment word public 'DATA'
  332. XIFB    ends
  333. XIF    segment word public 'DATA'
  334.     dd    ctor
  335. XIF    ends
  336. XIFE    segment word public 'DATA'
  337. XIFE    ends
  338.     else
  339. XIB    segment word public 'DATA'
  340. XIB    ends
  341. XI    segment word public 'DATA'
  342.     dw    ctor
  343. XI    ends
  344. XIE    segment word public 'DATA'
  345. XIE    ends
  346.     endif
  347.     endm
  348. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  349. ; Define function ctor as a static destructor
  350. static_dtor macro    dtor
  351.     if LCODE
  352. XCFB    segment word public 'DATA'
  353. XCFB    ends
  354. XCF    segment word public 'DATA'
  355.     dd    dtor
  356. XCF    ends
  357. XCFE    segment word public 'DATA'
  358. XCFE    ends
  359.     else
  360. XCB    segment word public 'DATA'
  361. XCB    ends
  362. XC    segment word public 'DATA'
  363.     dw    dtor
  364. XC    ends
  365. XCE    segment word public 'DATA'
  366. XCE    ends
  367.     endif
  368.     endm
  369.  
  370. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  371. ; Other more or less useful macros
  372. ; Commented out ones are obsolete
  373.  
  374. ;setESeqDS macro        ;set ES == DS, if not already true
  375. ;    ife ESeqDS
  376. ;    push    DS
  377. ;    pop    ES
  378. ;    endif
  379. ;    endm
  380.  
  381. .push    macro    list
  382.     irp    arg,<list>
  383.      push    arg
  384.     endm
  385.     endm
  386.  
  387. .pop    macro    list
  388.     irp    arg,<list>
  389.      pop    arg
  390.     endm
  391.     endm
  392.  
  393. _push    macro    list
  394.     irp    arg,<list>
  395.      push    arg
  396.     endm
  397.     endm
  398.  
  399. _pop    macro    list
  400.     irp    arg,<list>
  401.      pop    arg
  402.     endm
  403.     endm
  404.  
  405. ; Macros to save and restore regs destroyed by a function
  406.  
  407. .save    macro    list
  408.     if SAVESIDI
  409.     irp    arg,<list>
  410.      push    arg
  411.     endm
  412.     endif
  413.     endm
  414.  
  415. .restore macro    list
  416.     if SAVESIDI
  417.     irp    arg,<list>
  418.      pop    arg
  419.     endm
  420.     endif
  421.     endm
  422.  
  423. _save    macro    list
  424.     if SAVESIDI
  425.     irp    arg,<list>
  426.      push    arg
  427.     endm
  428.     endif
  429.     endm
  430.  
  431. _restore macro    list
  432.     if SAVESIDI
  433.     irp    arg,<list>
  434.      pop    arg
  435.     endm
  436.     endif
  437.     endm
  438.  
  439. ; Macros to save and restore ES, but only if ESeqDS is 1.
  440. ;pushES macro
  441. ;    if ESeqDS
  442. ;    push    ES
  443. ;    endif
  444. ;    endm
  445. ;
  446. ;popES    macro
  447. ;    if ESeqDS
  448. ;    pop    ES
  449. ;    endif
  450. ;    endm
  451.  
  452. clr    macro    list        ;clear a register
  453.     irp    reg,<list>
  454.      xor    reg,reg
  455.     endm
  456.     endm
  457.  
  458. tst    macro    reg
  459.     test    reg,reg
  460.     endm
  461.  
  462. jmps    macro    lbl
  463.     jmp    short    lbl
  464.     endm
  465.  
  466.     if @Version lt 600
  467.     ;For compatibility with MASM 5.10
  468. .if    macro    arg1,cond,arg2,lbl
  469.     cmp    arg1,arg2
  470.     j&cond    lbl
  471.     endm
  472.  
  473.     endif
  474.  
  475. _if    macro    arg1,cond,arg2,lbl
  476.     cmp    arg1,arg2
  477.     j&cond    lbl
  478.     endm
  479.  
  480. ;sob    macro    arg,lbl
  481. ;    ifidn    <arg>,<CX>
  482. ;     loop    lbl
  483. ;    else
  484. ;     dec    arg
  485. ;     jnz    lbl
  486. ;    endif
  487. ;    endm
  488.  
  489.  
  490.     ifdef _WINDOWS
  491.     extrn  DOS3CALL : far
  492.     endif
  493.  
  494. ifndef nobdos
  495. bdos    macro    func
  496.     ifnb    <func>
  497.     mov    AH,func
  498.     endif
  499.     ifndef _WINDOWS
  500.     int    21h
  501.     else
  502.         call    DOS3CALL
  503.     endif
  504.     endm
  505.  
  506. dpmi    macro    func
  507.     ifnb    <func>
  508.         mov    AX,func
  509.         endif
  510.     int    31h
  511.         endm
  512. else
  513. __bdos    macro    func
  514.     ifnb    <func>
  515.     mov    AH,func
  516.     endif
  517.     ifndef _WINDOWS
  518.     int    21h
  519.     else
  520.         call    DOS3CALL
  521.     endif
  522.     endm
  523.  
  524. __dpmi    macro    func
  525.     ifnb    <func>
  526.         mov    AX,func
  527.         endif
  528.     int    31h
  529.         endm
  530. endif
  531.  
  532. .retf    macro    val        ;force assembler to build a far return
  533.     ifnb    <val>
  534.      db    0CAh
  535.      dw    val
  536.     else
  537.      db    0CBh
  538.     endif
  539.     endm
  540.  
  541. _retf    macro    val        ;force assembler to build a far return
  542.     ifnb    <val>
  543.      db    0CAh
  544.      dw    val
  545.     else
  546.      db    0CBh
  547.     endif
  548.     endm
  549.  
  550. ; Sometimes MASM ignores my segment overrides.
  551. segES    macro
  552.     db    26h
  553.     endm
  554.  
  555. ; 32 bit negate
  556. neg32    macro    reg1,reg2
  557.      neg    reg1
  558.      neg    reg2
  559.      sbb    reg1,0
  560.     endm
  561.  
  562. ; Push immediate (reg is for scratch)
  563. pushi    macro    reg,value
  564.     if 0
  565.      push    value        ;for 286 code generation only
  566.     else
  567.      mov    reg,value
  568.      push    reg
  569.     endif
  570.     endm
  571.  
  572. ; Inc/dec BP if I8086V memory model
  573. incBP    macro
  574.     ifdef I8086V
  575.       inc    BP
  576.     endif
  577.     endm
  578.  
  579. decBP    macro
  580.     ifdef I8086V
  581.       dec    BP
  582.     endif
  583.     endm
  584.  
  585. WINENTER macro
  586.     ifdef _WINDOWS
  587.     ifndef I8086S
  588.     ifndef I8086C
  589.     inc    BP
  590.     endif
  591.     endif
  592.     endif
  593.         push    BP
  594.         mov    BP,SP
  595.     endm
  596.  
  597. WINLEAVE macro
  598.     pop    BP
  599.     ifdef _WINDOWS
  600.     ifndef I8086S
  601.     ifndef I8086C
  602.         dec    BP
  603.     endif
  604.     endif
  605.     endif
  606.     endm
  607.  
  608. WINENTER_NF macro
  609.     ifdef _WINDOWS
  610.     ifndef I8086S
  611.     ifndef I8086C
  612.         inc    BP
  613.         push    BP
  614.         mov    BP,SP
  615.     endif
  616.     endif
  617.     endif
  618.     endm
  619.  
  620. WINLEAVE_NF macro
  621.     ifdef _WINDOWS
  622.     ifndef I8086S
  623.     ifndef I8086C
  624.         pop    BP
  625.         dec    BP
  626.     endif
  627.     endif
  628.     endif
  629.     endm
  630.  
  631. else    ;M_I386
  632.  
  633.     .386P
  634.     .387
  635.  
  636. ifdef DOS386
  637. SEG_1ST_MEG    equ    034h    ;Selector of 1st Mbyte used by MSDOS
  638. SEG_SCREEN    equ    01ch    ;Selector for the video memory.
  639. SEG_PSP        equ    024H    ;PSP selector.
  640. SEG_DGROUP    equ    014H    ;DGROUP selector.
  641. SEG_ENV        equ    02cH    ;Selector of environment string.
  642.  
  643. OMF    equ    1
  644. COFF    equ    0
  645. ELF    equ    0
  646. _FLAT    equ    0        ;FLAT memory model
  647. _STDCALL equ    0        ;default to _stdcall
  648. _RETST0    equ    0        ;return floating point results in ST0
  649. endif    ;DOS386
  650.  
  651. ifdef X386
  652. _FLAT    equ    0
  653. _STDCALL equ    0
  654. _RETST0    equ    0
  655. endif
  656.  
  657. ifdef __OS2__
  658. OMF    equ    1
  659. COFF    equ    0
  660. ELF    equ    0
  661. _FLAT    equ    1    ;FLAT memory model
  662. _STDCALL equ    1
  663. _RETST0    equ    1
  664. _INLINE_8087 equ 1    ;defined if we always generate inline 8087 code
  665. endif
  666.  
  667. ifdef __NT__
  668. OMF    equ    1
  669. COFF    equ    0
  670. ELF    equ    0
  671. _FLAT    equ    1
  672. _STDCALL equ    0
  673. _RETST0    equ    1
  674. ;_NOCTOR equ    1    ;defined if no static constructors/destructors supported
  675. _INLINE_8087 equ 1    ;defined if we always generate inline 8087 code
  676. LONGDOUBLE equ 1    ;defined if memory model supports 80 bit long doubles
  677. endif
  678.  
  679. ifdef M_XENIX
  680. OMF    equ    1    ;Set to non-zero if OMF object format.
  681. COFF    equ    0    ;Set to non-zero if COFF object format.
  682. ELF    equ    0
  683. _FLAT    equ    0
  684. _STDCALL equ    0
  685. _RETST0    equ    0
  686. endif
  687.  
  688. ifdef M_UNIX
  689. ifndef OMF
  690. OMF     equ     0       ;Set to non-zero if OMF object format.
  691. endif
  692. ifdef M_ELF
  693. ifndef ELF
  694. ELF     equ     1       ;Set to non-zero if ELF object format.
  695. endif
  696. ifndef COFF
  697. COFF    equ     0       ;Set to non-zero if COFF object format.
  698. endif
  699. else    ;M_ELF
  700. ifndef ELF
  701. ELF     equ     0       ;Set to non-zero if ELF object format.
  702. endif
  703. ifndef COFF
  704. COFF    equ     1       ;Set to non-zero if COFF object format.
  705. endif
  706. endif    ;M_ELF
  707. _FLAT    equ    0
  708. _STDCALL equ    0
  709. _RETST0    equ    0
  710. endif
  711.  
  712. .ERRNZ    OMF AND COFF AND ELF
  713. .ERRE    OMF OR COFF OR ELF
  714.  
  715. I386    equ    1
  716.  
  717. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  718. ; Macros specific to each memory model in an attempt to make it easier
  719. ; to write memory model independent code.
  720. ;    begcode,endcode        Use to bracket code sections
  721. ;    begdata,enddata        Use to bracket data sections
  722. ;    begrcode,endrcode       Use to bracket real mode code sections 
  723. ;    begrdata,endrdata       Use to bracket real mode data sections 
  724. ;                               (Pharlap DOS386 only)
  725. ;    P            Offset on EBP to first argument on stack
  726. ;                Assuming EBP was pushed.
  727. ;    PS            Offset on ESP to first argument on stack
  728. ;                Assuming nothing was pushed on the stack.
  729. ;    ESeqDS            1 if ES == DS at all times
  730. ;    FSeqDS            1 if FS == DS at all times
  731. ;    GSeqDS            1 if GS == DS at all times
  732. ;    SSeqDS            1 if SS == DS at all times
  733. ;    SIZEPTR            # of bytes in a pointer
  734. ;    func            Declare a function as NEAR or FAR
  735. ;    callm            Call function as NEAR or FAR
  736. ;    LPTR            Large data model?
  737. ;    SPTR            Small data model?
  738.  
  739.  
  740. ;Macro for start and end of real mode code segment.
  741.  
  742. begcode_16 macro     
  743. __X386_CODESEG_16       segment para use16 public 'CODE'
  744.         assume ds:__X386_GROUP_16
  745.         assume cs:__X386_CODESEG_16
  746. endm
  747.  
  748. endcode_16 macro
  749. __X386_CODESEG_16       ends
  750. endm
  751.  
  752. begcode macro    module
  753.     if _FLAT
  754. _TEXT    segment para use32 public 'CODE'
  755.       assume    CS:FLAT,DS:FLAT,SS:FLAT
  756.     else
  757. _TEXT    segment dword public 'CODE'
  758.       assume    CS:_TEXT
  759.     endif
  760.     endm
  761.  
  762. endcode macro    module
  763. _TEXT    ENDS
  764.     endm
  765.  
  766. begdata macro
  767.  
  768. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  769. ; Set up segments for data
  770. ; Regular initialized data goes in _DATA
  771.  
  772. _DATA    segment dword public 'DATA'
  773. _DATA   ends
  774.  
  775.     ifndef _NOCTOR
  776. ;Function pointers to constructors
  777. if OMF
  778. XIFCB    segment dword public 'DATA'
  779. XIFCB    ends
  780. XIFU    segment dword public 'DATA'
  781. XIFU    ends
  782. XIFL    segment dword public 'DATA'
  783. XIFL    ends
  784. XIFM    segment dword public 'DATA'
  785. XIFM    ends
  786. XIFCE    segment dword public 'DATA'
  787. XIFCE    ends
  788. XIB     segment dword public 'DATA'
  789. XIB    ends
  790. XI    segment dword public 'DATA'
  791. XI    ends
  792. XIE    segment dword public 'DATA'
  793. XIE    ends
  794.  
  795. ;Function pointers to destructors
  796. XCB    segment dword public 'DATA'
  797. XCB    ends
  798. XC    segment dword public 'DATA'
  799. XC    ends
  800. XCE    segment dword public 'DATA'
  801. XCE    ends
  802.  
  803. ;Constant data, such as switch tables, go here.
  804.  
  805. CONST    segment dword public 'CONST'
  806. CONST    ends
  807. endif
  808. ;Segment for uninitialized data. This is set to 0 by the startup code/OS,
  809. ;so it does not consume room in the executable file.
  810.  
  811. _BSS    segment dword public 'BSS'
  812. _BSS    ends
  813.  
  814. if OMF
  815. ;Function pointers to destructors
  816. XOB    segment dword public 'BSS'
  817. XOB    ends
  818. XO    segment dword public 'BSS'
  819. XO    ends
  820. XOE    segment dword public 'BSS'
  821. XOE    ends
  822. endif
  823.  
  824. if OMF
  825. ifdef DOS386
  826. HUGE_BSS    segment dword public 'HUGE_BSS'
  827. HUGE_BSS    ends
  828. endif
  829.  
  830. EEND    segment dword public 'ENDBSS'
  831. EEND    ends
  832.  
  833. ifdef DOS386
  834. STACK    segment para stack 'STACK'
  835. STACK    ends
  836.  
  837. DGROUP    group    _DATA,XIFCB,XIFU,XIFL,XIFM,XIFCE,XOB,XO,XOE,XIB,XI,XIE,XCB,XC,XCE,CONST,_BSS,EEND,STACK
  838. else
  839. ifdef __OS2__
  840. STACK    segment para stack 'STACK'
  841. STACK    ends
  842.  
  843. DGROUP    group    _DATA,XIFCB,XIFU,XIFL,XIFM,XIFCE,XOB,XO,XOE,XIB,XI,XIE,XCB,XC,XCE,CONST,_BSS,EEND,STACK
  844. else
  845. ifdef __NT__
  846. STACK    segment para stack 'STACK'
  847. STACK    ends
  848.  
  849. DGROUP    group    _DATA,XIFCB,XIFU,XIFL,XIFM,XIFCE,XOB,XO,XOE,XIB,XI,XIE,XCB,XC,XCE,CONST,_BSS,EEND,STACK
  850. else
  851. DGROUP    group    _DATA,XIB,XI,XIE,XCB,XC,XCE,CONST,_BSS,EEND
  852. endif    ;__NT__
  853.  
  854. endif    ;__OS2__
  855. endif    ;DOS386
  856.  
  857. else    ;OMF
  858. if OMF 
  859. DGROUP    group    _DATA,XIFCB,XIFU,XIFL,XIFM,XIFCE,XOB,XO,XOE,XIB,XI,XIE,XCB,XC,XCE,CONST,_BSS
  860. else
  861. DGROUP    group    _DATA,_BSS
  862. endif
  863. endif
  864.     endif
  865.  
  866. _DATA    segment
  867.     if _FLAT
  868.       assume DS:FLAT
  869.     else
  870.       assume DS:DGROUP
  871.     endif
  872.     endm
  873.  
  874. enddata macro
  875. _DATA    ends
  876.     endm
  877.  
  878. P    equ    8    ; Offset of start of parameters on the stack frame
  879.             ; From EBP assuming EBP was pushed.
  880. PS    equ    4    ; Offset of start of parameters on the stack frame
  881.  
  882.             ; From ESP assuming EBP was NOT pushed.
  883. ;; For Win32, ESeqDS should be TRUE
  884. ifdef _WIN32
  885. ESeqDS  equ     1
  886. else
  887. ESeqDS  equ     0
  888. endif
  889.  
  890. FSeqDS    equ    0
  891. GSeqDS    equ    0
  892. SSeqDS    equ    1
  893. SIZEPTR equ    4    ; Size of a pointer
  894. LPTR    equ    0
  895. SPTR    equ    1
  896. LCODE    equ    0
  897.  
  898. func    macro    name
  899. _&name    proc    near
  900.     ifndef name
  901. name    equ    _&name
  902.     endif
  903.     endm
  904.  
  905. callm    macro    name
  906.     call    _&name
  907.     endm
  908.  
  909. ;Macros to replace public, extrn, and endp for C-callable assembly routines,
  910. ; and to define labels: c_label defines labels,
  911. ; c_public replaces public, c_extrn replaces extrn, and c_endp replaces endp
  912.  
  913. c_name    macro    name
  914.     name equ _&name
  915.     endm
  916.  
  917. c_label macro    name
  918. _&name:
  919.     endm
  920.  
  921. c_public macro    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  922.     ifnb <a>            ;;Check for blank argument
  923.     public    _&a
  924.     a equ _&a
  925.       ifnb <b>
  926.     c_public b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  927.       endif
  928.     endif
  929.     endm
  930.  
  931. c_extrn macro    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  932.     ifnb <a>            ;;Check for blank argument
  933.     extrn    _&a:b
  934.     a equ _&a
  935.       ifnb <c>
  936.     c_extrn c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  937.       endif
  938.     endif
  939.     endm
  940.  
  941.  
  942. c_endp    macro    name
  943. _&name    ENDP
  944.     endm
  945.  
  946. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  947. ; Define function ctor as a static constructor
  948. static_ctor macro    ctor
  949.   ifndef _NOCTOR
  950.   if 1
  951. XIFCB    segment dword public 'DATA'
  952. XIFCB    ends
  953. XIFU    segment dword public 'DATA'
  954.     ;dd    ctor
  955. XIFU    ends
  956. XIFL    segment dword public 'DATA'
  957.     ;dd    ctor
  958. XIFL    ends
  959. XIFM    segment dword public 'DATA'
  960.     if _FLAT
  961.     dd    offset FLAT:ctor
  962.     else
  963.     dd    ctor
  964.     endif
  965. XIFM    ends
  966. XIFCE    segment dword public 'DATA'
  967. XIFCE    ends
  968.     else
  969. XIB    segment dword public 'DATA'
  970. XIB    ends
  971. XI    segment dword public 'DATA'
  972.     if _FLAT
  973.     dd    offset FLAT:ctor
  974.     else
  975.     dd    ctor
  976.     endif
  977. XI    ends
  978. XIE    segment dword public 'DATA'
  979. XIE    ends
  980.   endif
  981.   endif
  982.     endm
  983. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  984. ; Define function dtor as a static destructor
  985. static_dtor macro    dtor
  986.   ifndef _NOCTOR
  987.   if 1
  988. XOB    segment dword public 'DATA'
  989. XOB    ends
  990. XO    segment dword public 'DATA'
  991.     if _FLAT
  992.     dd    offset FLAT:dtor
  993.     else
  994.     dd    dtor
  995.     endif
  996. XO    ends
  997. XOE    segment dword public 'DATA'
  998. XOE    ends
  999.   else
  1000. XCB    segment dword public 'DATA'
  1001. XCB    ends
  1002. XC    segment dword public 'DATA'
  1003.     if _FLAT
  1004.     dd    offset FLAT:dtor
  1005.     else
  1006.     dd    dtor
  1007.     endif
  1008.  
  1009. XC    ends
  1010. XCE    segment dword public 'DATA'
  1011. XCE    ends
  1012.   endif
  1013.   endif
  1014.     endm
  1015. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1016. ; Other more or less useful macros
  1017.  
  1018. ;Aligns the code on dword boundary for max speed.
  1019. _align    macro
  1020.     ;MASM 6.0 has a bug where ALIGN 4 produces bogus code when
  1021.     ;2 or 3 bytes of filler are necessary.
  1022.     ifdef @Version
  1023.       if @Version eq 600
  1024.         if 0
  1025.         if    (($ - _TEXT) and 3) eq 1
  1026.         db    08Bh,0C0h        ;MOV EAX,EAX
  1027.         nop
  1028.         else
  1029.         if    (($ - _TEXT) and 3) eq 2
  1030.         db    08Bh,0C0h        ;MOV EAX,EAX
  1031.         else
  1032.         if    (($ - _TEXT) and 3) eq 3
  1033.         nop
  1034.         endif
  1035.         endif
  1036.         endif
  1037.         endif
  1038.       else
  1039.         align    4            ;for MASM 5.1
  1040.       endif
  1041.     else
  1042.         align    4            ;for 386ASM
  1043.     endif
  1044.     endm
  1045.  
  1046. _push    macro    list
  1047.     irp    arg,<list>
  1048.      push    arg
  1049.     endm
  1050.     endm
  1051.  
  1052. _pop    macro    list
  1053.     irp    arg,<list>
  1054.      pop    arg
  1055.     endm
  1056.     endm
  1057.  
  1058. _if    macro    arg1,cond,arg2,lbl
  1059.     cmp    arg1,arg2
  1060.     j&cond    lbl
  1061.     endm
  1062.  
  1063. _ifs    macro    arg1,cond,arg2,lbl
  1064.     cmp    arg1,arg2
  1065.     j&cond    short lbl
  1066.     endm
  1067.  
  1068. clr    macro    list        ;clear a register
  1069.     irp    reg,<list>
  1070.      xor    reg,reg
  1071.     endm
  1072.     endm
  1073.  
  1074. jmps    macro    lbl
  1075.     jmp    short    lbl
  1076.     endm
  1077.  
  1078. tst    macro    reg
  1079.     test    reg,reg
  1080.     endm
  1081.  
  1082. ifndef nobdos
  1083.  
  1084. ifdef DOS386
  1085. bdos    macro    func        ;DOS system call.
  1086.     ifnb    <func>
  1087.      mov    AH,func
  1088.     endif
  1089.     int    21h
  1090.     endm
  1091. endif ;DOS386
  1092.  
  1093. ifdef X386
  1094. bdos    macro    func        ;DOS system call.
  1095.     ifnb    <func>
  1096.      mov    AH,func
  1097.     endif
  1098.     int    21h
  1099.     endm
  1100. endif ;DOS386
  1101.  
  1102. ifdef M_XENIX
  1103. bdos    macro    func        ; 386 XENIX system call.
  1104.     ifnb    <func>
  1105.      mov    EAX,func
  1106.     endif
  1107.  
  1108.     db 9ah            ; call far 0x7:0
  1109.     dd 0
  1110.     dw 07h
  1111.     endm
  1112. endif ;M_XENIX
  1113.  
  1114. ifdef M_UNIX
  1115. bdos    macro    func        ; UNIX system call.
  1116.     ifnb    <func>
  1117.      mov    EAX,func
  1118.     endif
  1119.  
  1120.     db 9ah            ; call far 0x7:0
  1121.     dd 0
  1122.     dw 07h
  1123.     endm
  1124. endif ;M_UNIX
  1125.  
  1126. endif ;nobdos
  1127.  
  1128. _retf    macro    val        ;force assembler to build a far return
  1129.     ifnb    <val>
  1130.      db    0CAh
  1131.      dw    val
  1132.     else
  1133.      db    0CBh
  1134.     endif
  1135.     endm
  1136.  
  1137. _ret    macro    val        ;decide whether caller or callee cleans stack
  1138.     if _STDCALL
  1139.       ret    val
  1140.     else
  1141.       ret
  1142.     endif
  1143.     endm
  1144.  
  1145. ; Macros to save and restore regs destroyed by a function
  1146. ; Give the macro the list of registers used by the function:
  1147. ;    uses    <AX,BX,SI,DI>
  1148. ;
  1149. ; At exit to function use 'unuse':
  1150. ;    unuse    <DI,SI,BX,AX>
  1151.  
  1152. uses    macro    list
  1153.     irp    reg,<list>
  1154.      ifidn    <reg>,<ebx>    ;Save ebx.
  1155.      push    reg
  1156.      endif
  1157.  
  1158.      ifidn    <reg>,<EBX>    ;Save EBX (bug in ifidni).
  1159.      push    reg
  1160.      endif
  1161.  
  1162.      ifidn    <reg>,<esi>    ;Save esi.
  1163.      push    reg
  1164.      endif
  1165.  
  1166.      ifidn    <reg>,<ESI>
  1167.      push    reg
  1168.      endif
  1169.  
  1170.      ifidn    <reg>,<edi>    ;Save edi.
  1171.      push    reg
  1172.      endif
  1173.  
  1174.      ifidn    <reg>,<EDI>
  1175.      push    reg
  1176.      endif
  1177.  
  1178.      ifidn    <reg>,<bx>    ;Save bx.
  1179.      push    reg
  1180.      endif
  1181.  
  1182.      ifidn    <reg>,<BX>
  1183.      push    reg
  1184.      endif
  1185.  
  1186.      ifidn    <reg>,<si>    ;Save si.
  1187.      push    reg
  1188.      endif
  1189.  
  1190.      ifidn    <reg>,<SI>
  1191.      push    reg
  1192.      endif
  1193.  
  1194.      ifidn    <reg>,<di>    ;Save di.
  1195.      push    reg
  1196.      endif
  1197.  
  1198.      ifidn    <reg>,<DI>
  1199.      push    reg
  1200.      endif
  1201.  
  1202.      ifidn    <reg>,<ds>    ;Save ds.
  1203.      push    reg
  1204.      endif
  1205.  
  1206.      ifidn    <reg>,<DS>
  1207.      push    reg
  1208.      endif
  1209.  
  1210.      ifidn    <reg>,<es>    ;Save es.
  1211.      push    reg
  1212.      endif
  1213.  
  1214.      ifidn    <reg>,<ES>
  1215.      push    reg
  1216.      endif
  1217.     endm
  1218.     endm
  1219.  
  1220. unuse    macro    list
  1221.     irp    reg,<list>
  1222.      ifidn    <reg>,<ebx>    ;Restore ebx.
  1223.      pop    reg
  1224.      endif
  1225.  
  1226.      ifidn    <reg>,<EBX>
  1227.      pop    reg
  1228.      endif
  1229.  
  1230.      ifidn    <reg>,<esi>    ;Restore esi.
  1231.      pop    reg
  1232.      endif
  1233.  
  1234.      ifidn    <reg>,<ESI>
  1235.      pop    reg
  1236.      endif
  1237.  
  1238.      ifidn    <reg>,<edi>    ;Restore edi.
  1239.      pop    reg
  1240.      endif
  1241.  
  1242.      ifidn    <reg>,<EDI>
  1243.      pop    reg
  1244.      endif
  1245.  
  1246.      ifidn    <reg>,<bx>    ;Restore bx.
  1247.      pop    reg
  1248.      endif
  1249.  
  1250.      ifidn    <reg>,<BX>
  1251.      pop    reg
  1252.      endif
  1253.  
  1254.      ifidn    <reg>,<si>    ;Restore si.
  1255.      pop    reg
  1256.      endif
  1257.  
  1258.      ifidn    <reg>,<SI>
  1259.      pop    reg
  1260.      endif
  1261.  
  1262.      ifidn    <reg>,<di>    ;Restore di.
  1263.      pop    reg
  1264.      endif
  1265.  
  1266.      ifidn    <reg>,<DI>
  1267.      pop    reg
  1268.      endif
  1269.  
  1270.      ifidn    <reg>,<ds>    ;Restore ds.
  1271.      pop    reg
  1272.      endif
  1273.  
  1274.      ifidn    <reg>,<DS>
  1275.      pop    reg
  1276.      endif
  1277.  
  1278.      ifidn    <reg>,<es>    ;Restore es.
  1279.      pop    reg
  1280.      endif
  1281.  
  1282.      ifidn    <reg>,<ES>
  1283.      pop    reg
  1284.      endif
  1285.  
  1286.     endm
  1287.     endm
  1288.  
  1289.  
  1290. endif    ;M_I386
  1291.  
  1292. ; Executable type
  1293. EXE_DOS        equ    1        ; MSDOS
  1294. EXE_DOS16RM    equ    2        ; Rational 286 DOS Extender
  1295. EXE_ZPM        equ    4        ; ZPM 286 DOS Extender
  1296. EXE_PHAR386    equ    8        ; Pharlap 386 DOS Extender
  1297. EXE_DOSX    equ    010h        ; DOSX 386 DOS Extender
  1298. EXE_WINDOWS    equ    020h        ; Windows 3
  1299. EXE_OS2        equ    040h        ; OS/2 1.x
  1300. EXE_SCOUNIX    equ    080h        ; SCO Unix
  1301. EXE_OS2_2    equ    100h        ; OS/2 2.0
  1302. EXE_WINDOWSNT    equ    200h        ; Windows NT
  1303.  
  1304.